package org.NooLab.texx.components;

import java.util.Vector;

import org.NooLab.structures.DocumentMetaData;
import org.NooLab.texx.util.XmlStringHandling;
import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.objects.StringedObjects;
import org.NooLab.utilities.strings.StringsUtil;
import org.w3c.dom.Node;

public class TikaServerResults {

	String xText="",tikasrvTextData="";
	DocumentMetaData documentMetaData = new DocumentMetaData();
	
	XmlStringHandling xMsg = new XmlStringHandling();
	StringsUtil strgutil = new StringsUtil();
	DFutils fileutil = new DFutils();
	
	// ========================================================================
	public TikaServerResults( String xtext){
		xText = xtext;
	}
	// ========================================================================
	
	public TikaServerResults go() {
		//
		/* 
		String filename = "D:/temp/x.xml.txt";
		if (fileutil.fileexists(filename)){
			fileutil.deleteFile(filename);
		}
		fileutil.writeFileSimple(filename, xText) ; 
		*/
		
		getPlainTextFromTikaServerXmlReturns( xText );
		getTextMetaFromTikaServerXmlReturns( xText );
		
		return this;
	}
	

	private void getTextMetaFromTikaServerXmlReturns(String xtext) {
		// 
		String key, value, str , vstr,plainText = "", rootStr="tikaresults";
		Vector<Object> keys , values;
		Node node;
		int np=0;
		
		
		
		xMsg.setContentRoot( rootStr ) ;

		// <metadata> <mime value="" /> <language code="" /> <source url="" />";
				np = xtext.indexOf("<mime");
					
		str = xMsg.getSpecifiedInfo( xtext, "/metadata/mime", "value") ;
			  documentMetaData.setMime( str );
			  
		str = xMsg.getSpecifiedInfo( xtext, "/metadata/language", "code") ;
			  documentMetaData.setLanguage(str);
			  
		str = xMsg.getSpecifiedInfo( xtext, "/metadata/source", "url") ;
			  documentMetaData.setSource(str);
			  
		/*
			<metadata>
				<meta item="1" key="WPS-ARTICLEDOI" value="10.1111/j.1467-9213.2009.630.x" /> 
				<meta item="2" key="dcterms:modified" value="2009-06-10T10:24:45Z" /> 
				<meta item="3" key="meta:creation-date" value="2009-05-14T14:13:25Z" /> 
				<meta item="4" key="meta:save-date" value="2009-06-10T10:24:45Z" /> 
				<meta item="5" key="Content-Length" value="76438" /> 
				<meta item="6" key="WPS-PROCLEVEL" value="2" /> 
				<meta item="7" key="Last-Modified" value="2009-06-10T10:24:45Z" /> 
				<meta item="8" key="dcterms:created" value="2009-05-14T14:13:25Z" /> 
				<meta item="9" key="date" value="2009-05-14T14:13:25Z" /> 
				<meta item="10" key="modified" value="2009-06-10T10:24:45Z" /> 
				<meta item="11" key="xmpTPg:NPages" value="13" /> 
				<meta item="12" key="Creation-Date" value="2009-05-14T14:13:25Z" /> 
				<meta item="13" key="title" value="CONSCIOUS REFERENCE" /> 
				<meta item="14" key="created" value="Thu May 14 15:13:25 BST 2009" /> 
				<meta item="15" key="producer" value="PDFlib PLOP 3.0 (.NET/Win32)/Acrobat Distiller 5.0.5 (Windows)" /> 
				<meta item="16" key="Content-Type" value="application/pdf" /> 
				<meta item="17" key="xmp:CreatorTool" value="PostScript PDriver module 4.41" /> 
				<meta item="18" key="resourceName" value="CONSCIOUS REFERENCE.pdf" /> 
				<meta item="19" key="Last-Save-Date" value="2009-06-10T10:24:45Z" /> 
				<meta item="20" key="dc:title" value="CONSCIOUS REFERENCE" /> 

			</metadata>	   getItemsList(rawXmlStr, ":RootElement:"/exclude/type", "item", "name")  
		 */
		keys   = xMsg.getItemsList(xText, "/tikaresults/metadata", "meta", "key");
		
		values = xMsg.getItemsList(xText, "/tikaresults/metadata", "meta", "value");
		
		
		if ((keys!=null) && (keys.size()>0) && (keys.size()==values.size())){
			
			for (int i=0;i<keys.size();i++){
				
				key   = (String) keys.get(i);
				value = (String) values.get(i);
				
				np=-1;
				if ((key.toLowerCase().contains("length"))){
					// pdf: Content-Length, word?, text?
					vstr = value;
					if (strgutil.isNumericX(vstr)){
						np= strgutil.getNumFromStr(vstr, -2);
					}
				}
				documentMetaData.setLength(np);
				
				np=-1;
				if ((key.toLowerCase().contains("pages"))){
					// pdf: key="xmpTPg:NPages" value="13", word?, text?
					vstr = value;
					if (strgutil.isNumericX(vstr)){
						np= strgutil.getNumFromStr(vstr, -2);
					}
				}
				documentMetaData.setPages(np);
				
				if ((key.toLowerCase().contains("author"))){
					
				}
				// key="dc:title" value="CONSCIOUS REFERENCE" />
				// the doc's own title
				
				// the doc's own type definition  key="Content-Type" value="application/pdf" /> 
				
				
			}
		}
		
	}

	private void getPlainTextFromTikaServerXmlReturns(String xtext) {
		// 
		String str , plainText = "",rootStr="tikaresults";
		StringedObjects so = new StringedObjects();
		
		xMsg.setContentRoot( rootStr ) ;
		
		// <metadata> <mime value="" /> <language code="" /> <source url="" />";
		// <?xml version="1.0" ?> <tikaresults> <text> ... </text> 

		str = xMsg.getTextData( xtext, "/tikaresults", "", "/text").trim() ;
		  
		int p = str.indexOf("\n"); // these do not occur in encoded strings
		if ((str.startsWith("rO0ABX")) || (p>str.length()-3)){
			plainText = (String) so.decode(str);
		}else{
			plainText = str;
		}
		
		tikasrvTextData = plainText;
		
		so=null;
	}


	public String getPlainText() {
		return tikasrvTextData;
	}

	public DocumentMetaData getMeta() {
		return documentMetaData;
	}
	
	
}
